package cn.tedu.store.user.webapi.controller;

import cn.tedu.store.commons.pojo.user.dto.CodeLoginDTO;
import cn.tedu.store.commons.pojo.user.dto.UserLoginDTO;
import cn.tedu.store.commons.pojo.user.dto.UserRegistryDTO;
import cn.tedu.store.commons.pojo.user.vo.UserLoginInfoVO;
import cn.tedu.store.commons.web.JsonResult;
import cn.tedu.store.user.service.IUserService;
import cn.tedu.store.user.webapi.mapper.UserMapper;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Slf4j
@Api(tags = "用户模块")
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    @ApiOperation("用户账号密码登录")
    @ApiOperationSupport(order = 100)
    @PostMapping("/login")
    public JsonResult<String> login(UserLoginDTO userLoginDTO){
        log.debug("账号密码登录 参数:{}",userLoginDTO);
        String jwt = userService.userLogin(userLoginDTO);
        return JsonResult.ok(jwt);
    }

    @ApiOperation("用户注册")
    @ApiOperationSupport(order = 200)
    @PostMapping("/register")
    public JsonResult<Void> register(UserRegistryDTO userRegistryDTO){
        log.debug("用户注册 参数:{}",userRegistryDTO);
        userService.userRegister(userRegistryDTO);
        return JsonResult.ok();
    }

    @ApiOperation("手机号验证码登录")
    @ApiOperationSupport(order = 110)
    @PostMapping("/login/phone")
    public JsonResult<String> loginByPhone(CodeLoginDTO codeLoginDTO){
        log.debug("验证码登录 参数:{}",codeLoginDTO);
        String jwt = userService.codeLogin(codeLoginDTO);
        return JsonResult.ok(jwt);
    }

    @ApiOperation("发送验证码")
    @ApiOperationSupport(order = 120)
    @PostMapping("/send-code")
    public JsonResult<String> sendCode(@RequestBody String phone) throws TencentCloudSDKException {
        log.debug("phone: {}",phone);
        phone = phone.substring(0,11);
        log.debug("phone: {}",phone);
        userService.sendCode(phone);
        return JsonResult.ok("验证码已发送,请注意查收,有效期120s!");
    }

}
